package com.ybfenpan.controller.admin.shop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.shop.Goods;
import com.ybfenpan.service.shop.GoodsService;
import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class GoodsListController extends BaseController {
	@Resource
	private GoodsService goodsService;

	@RequestMapping("admin/shop/goodsList.shtml")
	public String list(HttpServletRequest request) throws Exception {
		HashMap formMap = getParameterMap(request);

		formMap.put("pagination.orderBy", " id desc"); // 排列
		List<Goods> list = this.goodsService.queryForList("select", null);
		System.out.println("list:" + list.size());
		Pagination page = goodsService.queryForPageList(formMap);

		request.setAttribute(Pagination.pageBeanName, page);
		return "admin/shop/goodsList";
	}

	/**
	 * 是否冻结
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("admin/shop/goodsActive.shtml")
	public String use(HttpServletRequest request) throws Exception {
		Map formMap = getParameterMap(request);
		int id = MapUtil.getIntValue(formMap, "id");
		if (0 < id) {
			Goods goods = goodsService.getByKey(id);
			Map mapGoods = new HashMap();
			mapGoods.put("id", id);

			if (goods.getIsActive().equals("F")) {

				mapGoods.put("isActive", "T");

			} else {

				mapGoods.put("isActive", "F");

			}
			goodsService.update(mapGoods);
		}
		return "redirect:goodsList.shtml";

	}

	@RequestMapping("admin/shop/goodsDelete.shtml")
	public String delete(HttpServletRequest request) throws Exception {
		Map formMap = getParameterMap(request);
		int id = MapUtil.getIntValue(formMap, "id");
		if (0 < id) {
			goodsService.delete(id);
		}
		return "redirect:goodsList.shtml";

	}

}
